package com.jeecms.wicool.dao.product.impl;

import com.google.common.base.Strings;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.product.VIPProductDao;
import com.jeecms.wicool.entity.product.VIPProduct;

/**
 * Created by cuiyanlei on 15/6/16.
 */
public class VIPProductDaoImpl extends HibernateBaseDao<VIPProduct, Integer> implements VIPProductDao {

    @Override
    protected Class<VIPProduct> getEntityClass() {
        return VIPProduct.class;
    }

    @Override
    public VIPProduct save(VIPProduct vipProduct) {
        getSession().save(vipProduct);
        return vipProduct;
    }

    @Override
    public VIPProduct deleteById(Integer id) {
        VIPProduct bean = get(id);
        if (bean != null) {
            getSession().delete(bean);
        }
        return bean;
    }

    @Override
    public VIPProduct updateByUpdater(Updater<VIPProduct> updater) {
        return super.updateByUpdater(updater);
    }


    @Override
    public VIPProduct findById(Integer id) {
        return get(id);
    }

    @Override
    public Pagination getPage(String productName, String userLevel, int pageNo, int pageSize) {
        Finder f = Finder.create(" select bean from VIPProduct bean where 1=1");
        if (!Strings.isNullOrEmpty(productName)) {
            f.append(" and bean.productName like :productName");
            f.setParam("productName", "%" + productName + "%");
        }

        if (!Strings.isNullOrEmpty(userLevel)) {
            f.append(" and bean.userLevel = :userLevel");
            f.setParam("userLevel", userLevel);
        }

        f.append(" order by bean.status,bean.updateTime,bean.userLevel desc ");

        return find(f, pageNo, pageSize);
    }
}
